const App = getApp();

import moment from '../../utils/moment'
import Dialog from '../../weapp/dialog/dialog'
import Toast from '../../weapp/toast/toast';
// components/list/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: Number,
    styleP: Boolean,
    strs: String,
    form:Object,
    cusPageIndex: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    str1: '<<',
    str2: '<',
    title: '我的客户',
    type: '',    //客户类型
    timeData: {},
    time: Number,
    remind_time: '',
    totalPages: "",
    clickIndex: 1,
    value1: 1,
    searchVal:1,
    deviceList: [],    //列表
    deviceTotal: 0,    //总数
    devicePages: 0,     //总页数
    deviceIndex: 1,      //当前页 
    searchLoading: false, //加载更多
    deviceNoData: false,  //暂无更多
    searchLoadingComplete: false, //加载到底部
    see_show: 0
  },
  pageLifetimes: {
    show: function () {
      try {
        var consumer_id = wx.getStorageSync('consumer_id')
        if (consumer_id) {
          const deviceList = this.data.deviceList.map(el => {
            if (el.id === consumer_id) el.is_see = 1
            return el
          })
          this.setData({ deviceList: deviceList }, () => {
            wx.removeStorageSync('consumer_id')
          })
        }
      } catch (e) {
        console.error("e:" + e)
      }
    }
  },
  lifetimes: {
    ready: function () {
      // this.getCustomersList()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //分页逻辑
    handleSelect(e) {
      let index = e.currentTarget.dataset.value
      let status = e.currentTarget.dataset.status
      console.log(e)

      if (status == 'tree') {
        this.setData({
          value1: this.data.value1 + 1,
        })
      } else if (status == 'one' && index != 1) {
        this.setData({
          value1: this.data.value1 - 1
        })
      }
      this.setData({
        clickIndex: index,
        deviceList: [],
        searchVal:index
      })
      console.log(this.data.value1)
      this.getPbCustList()
    },
    handlePrev(e) {
      let index = e.currentTarget.dataset.index
      this.setData({
        value1: this.data.value1 != 1 ? this.data.value1 - 1 : this.data.value1,
        clickIndex: this.data.value1 != 1 ? this.data.value1 - 1 : this.data.value1,
        deviceList: [],
        searchVal:this.data.value1
      })
      console.log(this.data.value1)
      this.getPbCustList()
    },
    handleNext(e) {
      this.setData({
        value1: this.data.value1 + 1,
        clickIndex: this.data.value1 + 1,
        deviceList: [],
        searchVal:this.data.value1
      })
      console.log(this.data.value1)
      this.getPbCustList()
    },
    handleToFirst(){
      this.setData({
        value1:1,
        clickIndex:1,
        deviceList: [],
        searchVal:1
      })
      console.log(this.data.value1)
      this.getPbCustList()
    },
    handleToEnd(){
      this.setData({
        clickIndex:this.data.devicePages,
        deviceList: [],
        searchVal:this.data.devicePages
      })
      console.log(this.data.value1)
      this.getPbCustList()
    },
    handleValue(e){
      this.setData({
        searchVal:e.detail.value
      })
    },
    handleToPage(){
      let searchVal =  parseInt(this.data.searchVal);
      if(searchVal- this.data.devicePages > 0 ){
        Toast.fail('输入的数字超过总页数')
        return false
      }
      this.setData({
        clickIndex:this.data.searchVal,
        value1:Number(this.data.searchVal),
        deviceList:[]
      })
      console.log(this.data.devicePages)
      this.getPbCustList()
    },
    // 分页逻辑--------END

    onChange(e) {
      this.setData({
        timeData: e.detail,
      });
      // console.log(e)
    },
    handleHomeToReport(e) {
      let { type, id, status, reportid } = e.currentTarget.dataset
      // if (status == 0) {
      wx.navigateTo({
        url: `../../pages/report/index?type=${type}&id=${id}&reportid=${reportid}&status=${status}`,
      })
      // }
    },
    toDetail: function (e) {
      let id = e.currentTarget.dataset.id
      let tabid = e.currentTarget.dataset.tabid
      let status = e.currentTarget.dataset.status

      if (status && tabid) {
        if ((tabid == 1 && (status == 0 || status == 1 || status == 3 || status == 5)) || (tabid == 2 && status == 2)) {
          wx.navigateTo({
            url: `../../pages/detail/index?id=${id}&post_index=${this.data.cusPageIndex}&tabid=${tabid}`,
          })
        }
      } else {
        wx.navigateTo({
          url: `../../pages/detail/index?id=${id}&post_index=${this.data.cusPageIndex}&tabid=${tabid}`,
        })
      }
    },
    handleEditReoprt(e) {
      let id = e.currentTarget.dataset.remindid
      wx.navigateTo({
        url: `../../pages/report/editRep?id=${id}`,
      })
    },
    filterList(arr) {
      for (let index = 0; index < arr.length; index++) {
        arr[index].pic = ''
        arr[index].sort = ''
        // arr[index].create_time = arr[index].create_time.slice(5)
        arr[index].create_time = arr[index].create_time.substring(0, arr[index].create_time.length - 3);
        arr[index].sort = index + 1
        this.setData({
          time: arr[index].rollout_expire_time * 1000,
          remind_time: moment(arr[index].remind_time).format('M/DD')
        })
        switch (arr[index].level) {
          case 'A':
            arr[index].pic = '../../images/A.png'
            break;
          case 'B':
            arr[index].pic = '../../images/B.png'
            break;
          case 'C':
            arr[index].pic = '../../images/C.png'
            break;
          case 'D':
            arr[index].pic = '../../images/D.png'
            break;
          case 'T':
            arr[index].pic = '../../images/T.png'
            break;
          case 'K':
            arr[index].pic = '../../images/T.png'
            break;
          case 'W':
            arr[index].pic = '../../images/W.png'
            break;
          default:
            break;
        }
      }
    },
    //协防客户按钮操作
    handleHelpSubmit(e) {
      let _this = this
      let { id, api, type } = e.currentTarget.dataset
      let obj = {}
      obj = api == 'confirmHelpDefense' ? { h_id: id, confirm_type: type } : { h_id: id }
      Dialog.confirm({
        title: '标题',
        message: '是否进行该操作？',
      })
        .then(() => {
          App._post_form(`HelpDefense/${api}`, obj, function (result) {
            if (result.code === 0) {
              Toast.success('提交成功！')
              _this.getXfCustList()
            } else {
              Toast.fail(result.msg)
            }
          })
        })
        .catch(() => {
          // on cancel
        });
    },
    mobileStr(v) {
      return v ? String(v).substring(0, 3) + "********" : ''
    },

    /**
     * 获取人事列表数据
     */
    getInductionList(form) {
      let _this = this
      console.log(_this.data.deviceIndex)
      wx.showLoading({
        title: '加载中...',
      })
      App._post_form('Induction/getList', {
        // ...form,
        // page: _this.data.deviceIndex,
      }, function (result) {
        console.log(result);
        // return false
        wx.hideLoading()
        // _this.filterList(result.data.list)
        let Result = result.data.data
        //     let Result = result.data.list.map(el=>{
        //         const mobile = _this.mobileStr(el.mobile);
        //         return {...el, mobile}
        //      })
        // 响应无数据 则提示无数据
        if (_this.data.deviceIndex == 1 && Result.length == 0) {
          _this.setData({
            deviceNoData: true,
            searchLoading: false,
            searchLoadingComplete: false
          })
        }
        if (Result.length) {
          _this.data.deviceTotal = result.data.count
          _this.data.devicePages = result.data.total_page
          let deviceListTemp = [];
          if (_this.data.deviceIndex > 1) {
            deviceListTemp = _this.data.deviceList.concat(Result);
            var result = [];
            var obj = {};
            for (var i = 0; i < deviceListTemp.length; i++) {
              if (!obj[deviceListTemp[i].id]) {
                result.push(deviceListTemp[i]);
                obj[deviceListTemp[i].id] = true;
              }
            }
            deviceListTemp = result
            console.log(deviceListTemp)
            _this.setData({
              searchLoading: true, //"正在载入更多"显示
            });
          } else {
            deviceListTemp = Result;
          }
          // deviceListTemp.forEach(element => {

          // });
          _this.setData({
            deviceList: deviceListTemp, //获取数据数组
            deviceNoData: false //“没有数据”隐藏
          });
        }
      });
    },


    /**
     * 获取客户页客户列表数据
     */
    getCustList(form) {
      let _this = this
      console.log(_this.data.deviceIndex)
      wx.showLoading({
        title: '加载中...',
      })
      App._post_form('customer/getCustomerList', {
        ...form,
        page: _this.data.deviceIndex,
      }, function (result) {
        console.log(result);
        wx.hideLoading()
        _this.filterList(result.data.list)
        let Result = result.data.list
        //     let Result = result.data.list.map(el=>{
        //         const mobile = _this.mobileStr(el.mobile);
        //         return {...el, mobile}
        //      })
        // 响应无数据 则提示无数据
        if (_this.data.deviceIndex == 1 && Result.length == 0) {
          _this.setData({
            deviceNoData: true,
            searchLoading: false,
            searchLoadingComplete: false
          })
        }
        if (Result.length) {
          _this.data.deviceTotal = result.data.count
          _this.data.devicePages = result.data.total_page
          let deviceListTemp = [];
          if (_this.data.deviceIndex > 1) {
            deviceListTemp = _this.data.deviceList.concat(Result);
            var result = [];
            var obj = {};
            for (var i = 0; i < deviceListTemp.length; i++) {
              if (!obj[deviceListTemp[i].id]) {
                result.push(deviceListTemp[i]);
                obj[deviceListTemp[i].id] = true;
              }
            }
            deviceListTemp = result
            console.log(deviceListTemp)
            _this.setData({
              searchLoading: true, //"正在载入更多"显示
            });
          } else {
            deviceListTemp = Result;
          }
          // deviceListTemp.forEach(element => {

          // });
          _this.setData({
            deviceList: deviceListTemp, //获取数据数组
            deviceNoData: false //“没有数据”隐藏
          });
        }
      });
    },
    //协防客户列表
    getXfCustList(form) {
      let _this = this
      wx.showLoading({
        title: '加载中...',
      })
      App._post_form('HelpDefense/helpDefenseList', {
        ...form,
        page: _this.data.deviceIndex,
      }, function (result) {
        wx.hideLoading()
        _this.filterList(result.data.list)
        let Result = result.data.list
        // 响应无数据 则提示无数据
        if (_this.data.deviceIndex == 1 && Result.length == 0) {
          _this.setData({
            deviceNoData: true,
            searchLoading: false,
            searchLoadingComplete: false
          })
        }
        if (Result.length) {
          _this.data.deviceTotal = result.data.count
          _this.data.devicePages = result.data.total_page
          let deviceListTemp = [];
          if (_this.data.deviceIndex > 1) {
            deviceListTemp = _this.data.deviceList.concat(Result);
            var result = [];
            var obj = {};
            for (var i = 0; i < deviceListTemp.length; i++) {
              if (!obj[deviceListTemp[i].id]) {
                result.push(deviceListTemp[i]);
                obj[deviceListTemp[i].id] = true;
              }
            }
            deviceListTemp = result
            console.log(deviceListTemp)
            _this.setData({
              searchLoading: true, //"正在载入更多"显示
            });
          } else {
            deviceListTemp = Result;
          }
          _this.setData({
            deviceList: deviceListTemp, //获取数据数组
            deviceNoData: false //“没有数据”隐藏
          });
        }
      });
    },
    //公池客户列表
    getPbCustList() {
      let _this = this
      wx.showLoading({
        title: '加载中...',
      })
      App._post_form('Resource/getResourceList', {
        ..._this.data.form,
        page: _this.data.searchVal,
      }, function (result) {
        wx.hideLoading()
        _this.filterList(result.data.list)
        let Result = result.data.list
        _this.setData({
          devicePages: result.data.total_page
        })
        // 响应无数据 则提示无数据
        if (_this.data.deviceIndex == 1 && Result.length == 0) {
          _this.setData({
            deviceNoData: true,
            searchLoading: false,
            searchLoadingComplete: false
          })
        }
        if (Result.length) {
          _this.data.deviceTotal = result.data.count
          // _this.data.devicePages = result.data.total_page
          let deviceListTemp = [];
          if (_this.data.deviceIndex > 1) {
            deviceListTemp = _this.data.deviceList.concat(Result);
            // deviceListTemp =Array.from(new Set([..._this.data.deviceList,...Result]))
            var result = [];
            var obj = {};
            for (var i = 0; i < deviceListTemp.length; i++) {
              if (!obj[deviceListTemp[i].id]) {
                result.push(deviceListTemp[i]);
                obj[deviceListTemp[i].id] = true;
              }
            }
            deviceListTemp = result
            console.log(deviceListTemp)
            _this.setData({
              searchLoading: true, //"正在载入更多"显示
            });
          } else {
            deviceListTemp = Result;
          }
          _this.setData({
            deviceList: Result, //获取数据数组
            deviceNoData: false, //“没有数据”隐藏

          });
        }
      });
    },
    // 页面触底时执行
    reachBottom(action) {
      var _this = this;
      if (_this.data.deviceIndex < _this.data.devicePages) {
        _this.setData({
          deviceIndex: _this.data.deviceIndex + 1,   //每次触发触底事件，把deviceIndex+1
        });
        if (_this.data.strs == 'home_page') {
          _this.getCustomersList(action);
          return
        }
        if (_this.data.strs == 'cus_page' && _this.data.cusPageIndex == 0) {
          _this.getCustList(action)
          return
        }
        if (_this.data.strs == 'cus_page' && _this.data.cusPageIndex == 1) {
          _this.getXfCustList(action)
          return
        }
        if (_this.data.strs == 'cus_page' && _this.data.cusPageIndex == 2) {
          // _this.getPbCustList(action)
          return
        }

      } else if (_this.data.deviceIndex == _this.data.devicePages) {
        _this.setData({
          searchLoadingComplete: true, //“已加载全部”显示
          searchLoading: false, //"正在载入更多"隐藏
        });
      }
    }
  }
})
